using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data.OleDb;
using System.Data;

namespace DAOLayer
{
    public class HoaDonDao
    {
        public static void Them(HoaDonDto hd)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            // B3: Tao chuoi strSQL thao tac CSDL
            string strSQL = "Insert into HOADON ( NgayThanhToan,TriGia,TenKhachHang,DiaChi ) values(?,?,?,?)";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@NgayThanhToan", OleDbType.DBDate);
            cmd.Parameters["@NgayThanhToan"].Value = hd.NgayThanhToan;
            cmd.Parameters.Add("@TriGia", OleDbType.Integer);
            cmd.Parameters["@TriGia"].Value = hd.TriGia;
            cmd.Parameters.Add("@TenKhachHang", OleDbType.WChar);
            cmd.Parameters["@TenKhachHang"].Value = hd.TenKhachHang;
            cmd.Parameters.Add("@DiaChi", OleDbType.WChar);
            cmd.Parameters["@DiaChi"].Value = hd.DiaChi;
           
            cmd.ExecuteNonQuery();

            strSQL = "Select @@IDENTITY";
            cmd = new OleDbCommand(strSQL, cn);
            hd.MaHoaDon = Convert.ToInt32(cmd.ExecuteScalar());
            // B5: Dong ket noi CSDL
            cn.Close();
        }
        public static  double TinhTongDoanhThuCuaThang(int thang)//int thang)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            // B3: Tao chuoi strSQL thao tac CSDL
            string strSQL = "Select Sum(TriGia) From HOADON where Month(NgayThanhToan) = '" + thang + "'";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
           
            double tt = Convert.ToDouble(cmd.ExecuteScalar());
            cn.Close();
            return tt;
        }
        public static DataTable LayTatCaCacThangThanhToan()
        {
            DataTable dt = new DataTable();
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            // B3: Tao chuoi strSQL thao tac CSDL
            string strSQL = "Select Month(NgayThanhToan) From HOADON ";
            //'B4: Thuc thi chuoi strSQL
            OleDbDataAdapter da = new OleDbDataAdapter(strSQL, cn);
            da.Fill(dt);
            // 'B5: Dong ket noi CSDL
            cn.Close();
            return dt;


           
        }

        
    }
}
